home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / DTS QT Utilities.Aug-95 / Projects & Test Apps / MovieGWorlds / MovieGWorlds.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-17  |  9.2 KB  |  279 lines  |  [TEXT/MPCC]

  1. // INCLUDES
  2. #include "MovieGWorlds.h"
  3.  
  4.  
  5. // GLOBALS
  6. Movie                     gMovie = NULL;
  7. CWindowPtr                 gWindow = NULL;
  8. TimeValue                    gCurrentTime = 0L;
  9. TimeValue                    gNextTime = 0L;
  10. long                        gSampleCount = 0L;
  11. long                        gDrawingTime = 0L;
  12.  
  13.  
  14. // ______________________________________________________________________
  15. //• MAIN -- Starting Point.
  16. void main(void) {
  17.     EventRecord     anEvent;
  18.     OSErr        anErr = noErr;
  19.     OSType        mediaType = VideoMediaType;
  20.     
  21.     //• Initialize the Mac environment.
  22.     InitMacEnvironment(6);
  23.     InitializeQTEnvironment();
  24.     if( InitializeMovie() != noErr) ExitToShell();
  25.  
  26.     //•  Get information about the first video sample (frame).
  27.     anErr =  QTUGetStartPointOfFirstVideoSample(gMovie,&gCurrentTime); DebugAssert(anErr = noErr);
  28.     if(gCurrentTime  == -1) ExitToShell();    // no frames at all
  29.     gSampleCount++;
  30.     
  31.     //• Set to Window GWorld, scroll to the first video sample.
  32.     SetGWorld(gWindow, NULL);
  33.     
  34.     gDrawingTime = TickCount();
  35.     anErr = QTUScrollToNextVideoSample(gMovie, gCurrentTime, gCurrentTime ); DebugAssert(anErr == noErr); // draw first frame
  36.     gDrawingTime = TickCount() - gDrawingTime;
  37.     
  38.     //• Draw information on the screen.
  39.     DrawInformation();
  40.     DrawUsageInformation();
  41.     
  42.     //• Event loop.
  43.     for(;;) {
  44.         WaitNextEvent(everyEvent, &anEvent, 60, NULL);
  45.         
  46.         if(anEvent.what == mouseDown)
  47.             break;
  48.         
  49.         if(anEvent.what == keyDown) {                // Get next sample, stop if the time value for next sample is -1 (no more samples)
  50.             GetMovieNextInterestingTime(gMovie, nextTimeMediaSample, 1, &mediaType, gCurrentTime, 0, &gNextTime, NULL);
  51.             if(gNextTime == -1) break;
  52.             
  53.             gSampleCount++;
  54.             SetGWorld(gWindow, NULL);
  55.             
  56.             gDrawingTime = TickCount();
  57.             anErr = QTUScrollToNextVideoSample(gMovie, gCurrentTime, gNextTime); DebugAssert(anErr == noErr);
  58.             gDrawingTime = TickCount() - gDrawingTime;
  59.  
  60.             gCurrentTime = gNextTime;
  61.             DrawInformation();
  62.         }
  63.     }
  64. }
  65.  
  66.  
  67. // ______________________________________________________________________
  68. //• InitMacEnvironment -- Initialize the Mac Toolbox.
  69. void InitMacEnvironment(long nMasters) {
  70.     long i;
  71.  
  72.     MaxApplZone();
  73.     
  74.     for(i = 0; i <nMasters; i++)
  75.         MoreMasters();
  76.     
  77.     InitGraf(&qd.thePort);
  78.     InitFonts();
  79.     InitWindows();
  80.     InitMenus();
  81.     FlushEvents(everyEvent, 0);
  82.     TEInit();
  83.     InitCursor();
  84.     InitDialogs(NULL);
  85. }
  86.  
  87.  
  88. // ______________________________________________________________________
  89. //• InitializeQTEnvironment -- Initialize the QuickTime movie toolbox parts.
  90. void InitializeQTEnvironment(void) {
  91.     OSErr anErr = noErr;
  92.     
  93.     if( !QTUIsQuickTimeInstalled() ) {
  94.         DebugStr("\pThe QuickTime extension is not present in this system");
  95.         ExitToShell();
  96.     }
  97.  
  98.     if( (QTUGetQTVersion() >> 16 ) < 0x200 ) {
  99.         DebugStr("\pWe need QT 2.0 or higher due to APIs used in this sample, consult the sources (exit).");
  100.         ExitToShell();
  101.     }
  102.  
  103. #if powerc    
  104.     if( !QTUIsQuickTimeCFMInstalled() ) {
  105.         DebugStr("\pThe QuickTime PowerPlug extension is not available (exit)");
  106.         ExitToShell();    
  107.     }                            
  108. #endif 
  109.     
  110.     anErr = EnterMovies(); DebugAssert(anErr == noErr);
  111.     if(anErr != noErr) {
  112.         DebugStr("\pProblems with Entermovies, returning errors (exit)");
  113.         ExitToShell();
  114.     }
  115. }
  116.  
  117.  
  118. // ______________________________________________________________________
  119. //• InitializeMovie -- Initialize the needed movie parts for the offscreen handling.
  120. OSErr InitializeMovie(void) {
  121.     OSErr         anErr = noErr;
  122.     Rect             windowBounds = { kWindowYStart, kWindowXStart, kWindowHeigth, kWindowLength};
  123.     Rect            movieRect = {0, 0, 0, 0};
  124.     
  125.     //• Create the window we will use.
  126.     gWindow = (CWindowPtr) NewCWindow(NULL, &windowBounds, "\pMovie GWorld Test", true, noGrowDocProc,
  127.                                             NULL, false, 0); DebugAssert(gWindow != NULL);
  128.     SetPort((GrafPtr)gWindow);
  129.     
  130.     //• Get the movie.
  131.     anErr = QTUSimpleGetMovie(&gMovie); DebugAssert(anErr == noErr);
  132.     if(anErr)  goto Closure;
  133.     
  134.     //• Adjust the  movie box values.
  135.     GetMovieBox(gMovie, &movieRect); 
  136.     OffsetRect(&movieRect, -movieRect.left, -movieRect.top);
  137.     SetMovieBox(gMovie, &movieRect); 
  138.     AlignWindow((WindowPtr)gWindow, false,  &movieRect, NULL);
  139.  
  140.     //• Specify the movie GWorld.
  141.     SetMovieGWorld(gMovie, (GWorldPtr)gWindow, NULL);
  142.     anErr = GetMoviesError(); DebugAssert(anErr != noErr);
  143.  
  144. Closure:    
  145.     return anErr;
  146. }
  147.  
  148.  
  149. // ______________________________________________________________________
  150. //• QTUScrollToNextVideoSample -- Scroll offscreen from one video sample to the next.
  151. pascal OSErr ScrollToNextVideoSample(Movie theMovie, TimeValue fromTimePoint, TimeValue toTimePoint) {
  152.     OSErr         anErr = noErr;
  153.     GWorldPtr        frameGWorld1 = NULL;
  154.     GWorldPtr        frameGWorld2 = NULL;
  155.     PixMapHandle    pixMap1 = NULL;
  156.     PixMapHandle    pixMap2    = NULL;
  157.     CGrafPtr        aSavedPort, moviePort;
  158.     GDHandle        aSavedGDevice, movieGDevice;
  159.     CTabHandle    colorTable;
  160.     short        screenDepth = 0;
  161.     short        screenSize = 0;
  162.     Rect            movieRect, sourceRect, destinationRect;
  163.     RgnHandle        scrollRegion = NULL;
  164.     RgnHandle        clipRegion = NULL;
  165.     short        nSteps;
  166.         
  167.     DebugAssert(theMovie != NULL); if(theMovie == NULL) goto Closure;
  168.     
  169.     //• Store away current portrect and Gdevice, get pixel sizes and color table for GWorld creation purposes.
  170.     GetGWorld(&aSavedPort, &aSavedGDevice);
  171.     
  172.     GetMovieGWorld(theMovie, &moviePort, &movieGDevice);
  173.     screenDepth = (**(**aSavedGDevice).gdPMap).pixelSize;
  174.     colorTable = (**(**aSavedGDevice).gdPMap).pmTable;
  175.     
  176.     //• Adjust the movie box.
  177.      GetMovieBox(theMovie, &movieRect);  // If you want to offset by 10,10: OffsetRect(&movieRect, 10 -movieRect.left, 10 - movieRect.top);
  178.      SetMovieBox(theMovie, &movieRect);
  179.     
  180.     //• Create two GWorlds for dual screen writing and possible scrolling transition effects. Lock down pixmaps.
  181.     anErr = NewGWorld(&frameGWorld1, screenDepth, &movieRect, colorTable, NULL, 0); DebugAssert(anErr == noErr);
  182.     if(anErr != noErr) goto Closure;
  183.     anErr = NewGWorld(&frameGWorld2, screenDepth, &movieRect, colorTable, NULL, 0); DebugAssert(anErr == noErr);
  184.     if(anErr != noErr) goto Closure;
  185.     
  186.     pixMap1 = GetGWorldPixMap(frameGWorld1);  if(!LockPixels(pixMap1)) goto Closure;
  187.     pixMap2 = GetGWorldPixMap(frameGWorld2);  if(!LockPixels(pixMap2)) goto Closure;
  188.     
  189.     //• Draw first video sample (frame) to GWorld number 1.
  190.     SetMovieGWorld(theMovie, frameGWorld1, GetGWorldDevice(frameGWorld1));
  191.     SetMovieTimeValue(theMovie, fromTimePoint);
  192.     UpdateMovie(theMovie); MoviesTask(theMovie, 0);
  193.     
  194.     //• Draw second video sample (frame) to GWorld number 2.
  195.     SetMovieGWorld(theMovie, frameGWorld2, GetGWorldDevice(frameGWorld2));
  196.     SetMovieTimeValue(theMovie, toTimePoint); 
  197.     UpdateMovie(theMovie); MoviesTask(theMovie, 0);
  198.     
  199.     //• Create scroll region and store away the current clip region.
  200.     scrollRegion = NewRgn(); DebugAssert(scrollRegion != NULL);
  201.     if(scrollRegion == NULL) goto Closure;
  202.     
  203.     clipRegion = NewRgn(); DebugAssert(clipRegion != NULL);
  204.     if(clipRegion == NULL) goto Closure;
  205.     GetClip(clipRegion); ClipRect(&movieRect);
  206.     
  207.     //• Create the scroll effect.
  208.     screenSize = movieRect.right - movieRect.left;
  209.     
  210.     for(nSteps = 10; nSteps <= screenSize; nSteps += 10)  {
  211.         SetGWorld( frameGWorld1, NULL);
  212.         
  213.         ScrollRect(&movieRect, -10, 0, scrollRegion);
  214.         SetRect(&sourceRect, movieRect.left, movieRect.top, 
  215.                 movieRect.left + nSteps, movieRect.bottom);
  216.         SetRect(&destinationRect, movieRect.right - nSteps,
  217.                 movieRect.top, movieRect.right, movieRect.bottom);
  218.         
  219.         CopyBits( (BitMap *) *pixMap2, (BitMap *) *pixMap1, &sourceRect, &destinationRect,
  220.                     srcCopy, NULL );                // blit from frameGWorld2 to frameGWorld1
  221.         DebugAssert(QDError() == noErr);
  222.         
  223.         SetGWorld(aSavedPort, aSavedGDevice);
  224.         CopyBits( (BitMap *) *pixMap1, (BitMap *) &aSavedPort->portPixMap, &movieRect,
  225.                     &movieRect, srcCopy, NULL );       // blit from frameGWorld1 to screen pixmap
  226.         DebugAssert(QDError() == noErr);
  227.     }
  228.  
  229.     //• Unlock pixels, restore the original clip region.    
  230.     UnlockPixels(pixMap1); UnlockPixels(pixMap2);
  231.     SetClip(clipRegion);
  232.     
  233.     //• Closure. Clean up if we have handles.
  234. Closure:    
  235.     if(frameGWorld1 != NULL)         DisposeGWorld(frameGWorld1);
  236.     if(frameGWorld2 != NULL)         DisposeGWorld(frameGWorld2);
  237.     if(scrollRegion != NULL)             DisposeRgn(scrollRegion);
  238.     if(clipRegion != NULL)             DisposeRgn(clipRegion);
  239.  
  240.     SetMovieGWorld(theMovie, moviePort, movieGDevice);
  241.     SetGWorld(aSavedPort, aSavedGDevice);    
  242.  
  243.     return anErr;
  244. }
  245.  
  246.  
  247. // ______________________________________________________________________
  248. //• DrawInformation -- Provide information about what video sample is currently drawn.
  249. void DrawInformation(void) {
  250.     Str255     theString;
  251.     Rect        eraseArea = {kDrawValuesY-20, kDrawValuesX -20, kDrawValuesY+50, kDrawValuesX+50};
  252.     
  253.     EraseRect(&eraseArea); 
  254.  
  255.     //• Display what frame (sample) we are drawing.
  256.     MoveTo(kDrawTextX, kDrawTextY); 
  257.     TextFace(bold); TextSize(9);
  258.     DrawString("\pFrame drawn (number):");
  259.     MoveTo(kDrawValuesX, kDrawValuesY);
  260.     NumToString(gSampleCount, theString); 
  261.     DrawString(theString);
  262.     
  263.     //• Display how long it took to call the QTUScrollToNextVideoSample function.
  264.     MoveTo(kDrawTextX, 100);
  265.     TextFace(bold); TextSize(9);
  266.     DrawString("\pOperation took (ticks): ");
  267.     MoveTo(kDrawValuesX, 100);
  268.     NumToString(gDrawingTime, theString);
  269.     DrawString(theString);
  270. }
  271.  
  272. // ______________________________________________________________________
  273. //• DrawUsageInformation -- Draw instructions how to use this simple program.
  274. void DrawUsageInformation(void) {
  275.     TextFace(normal); TextSize(9);
  276.     MoveTo(kDrawTextX, 200); DrawString("\pHit a key to switch to next video sample (frame).");
  277.     MoveTo(kDrawTextX, 220); DrawString("\pClick on the mouse to terminate program.");
  278. }
  279.